package com.sangmo.fts.process.factoring.service;

import com.sangmo.boot.framework.common.errors.ServiceException;
import com.sangmo.boot.framework.common.errors.StandardErrors;
import com.sangmo.boot.framework.common.utils.DateUtils;
import com.sangmo.boot.framework.data.idm.IdentityManager;
import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.basic.service.FileUploadService;
import com.sangmo.fts.common.authority.service.AuthorityEntityService;
import com.sangmo.fts.process.factoring.interfaces.FfContractCreation;
import com.sangmo.fts.process.factoring.interfaces.FfContractItemCreation;
import com.sangmo.fts.process.factoring.model.FfContract;
import com.sangmo.fts.process.factoring.model.FfContractItem;
import com.sangmo.fts.process.factoring.repo.FfContractRepo;
import com.sangmo.fts.process.receivable.model.RpfContract;
import com.sangmo.fts.process.receivable.model.RpfContractItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class FfContractEntityService extends AuthorityEntityService<FfContract> {

    @Autowired
    private FfContractRepo ffContractRepo;

    @Autowired
    private IdentityManager identityManager;

    @Autowired
    private FileUploadService fileUploadService;

    @Autowired
    private FfContractItemEntityService ffContractItemEntityService;

    @Override
    protected IDataViewRepo<FfContract> getRepo() {
        return ffContractRepo;
    }

    @Override
    protected void onCreate(FfContract entity, Object bo) {
        super.onCreate(entity, bo);
        entity.setCode(DateUtils.format(new Date(), "yyyyMMdd") + "3" + identityManager.next("CSF_CONTRACT", 5));
    }

    @Override
    protected void afterCreated(FfContract entity, Object bo) {
        super.afterCreated(entity, bo);
        FfContractCreation creation = (FfContractCreation) bo;
        if (null != creation.getBuyerConfirmDoc()) {
            fileUploadService.updateRef(creation.getBuyerConfirmDoc(), ""+entity.getId(), "FfContract.Buyer");
        }
        List<FfContractItem> contractItems = buildContractItems(entity.getId(), creation.getContractItems());
        ffContractItemEntityService.saveAll(contractItems);
    }

    private List<FfContractItem> buildContractItems(Long contractId, List<FfContractItemCreation> contractItemCreations) {
        List<FfContractItem> contractItems = new ArrayList<>();
        for (FfContractItemCreation creation : contractItemCreations) {
            FfContractItem item = new FfContractItem();
            item.setContractId(contractId);
            item.setGoodsId(creation.getGoodsId());
            item.setQuantity(creation.getQuantity());
            contractItems.add(item);
        }
        return contractItems;
    }

    @Override
    protected void onDelete(FfContract entity) {
        super.onDelete(entity);
        if (entity.isBuyerConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.ffContract.remove.permit.disabled");
        }
    }

    protected void buyerConfirm(FfContract entity) {
        entity.setBuyerConfirm(true);
        this.save(entity);
    }

    protected void sellerConfirm(FfContract entity) {
        entity.setSellerConfirm(true);
        this.save(entity);
    }
}
